define([
        'angular',
        'utils/route-config',
        'angularRoute',
        'ngFileUpload',
        'ngSortable',
        'ngCkeditor',
        'ngUiTree',
        'ngDate',
    ],

    function (angular, routeConfig) {

        return angular.module('myApp', ['ngRoute', 'ngFileUpload', 'ng-sortable', 'ckeditor', 'ui.tree', 'datetimepicker'], function ($provide, $compileProvider, $controllerProvider, $filterProvider) {

            routeConfig.setProvide($provide); //for services
            routeConfig.setCompileProvider($compileProvider);  //for directives
            routeConfig.setControllerProvider($controllerProvider); //for controllers
            routeConfig.setFilterProvider($filterProvider); //for filters
        }).controller('menu', ['$scope', '$location', '$http', '$timeout', function ($scope, $location, $http, $timeout) {
            // putting $location as a $scope instance variable
            // so that we could set "active" class based on current $location.path



            $scope.location = $location;
            $http({
                method:"GET",
                url: "/login/auth",
            }).then(function(data){
                if(data && data.data && data.data.code==0 && data.data.user){
                    $scope.user = data.data.user;
                    $scope.$broadcast('auth', $scope.user);
                }else{
                    setTimeout(function(){
                        window.location ="/login";
                    },0);
                }

            });





            $(function () {
                $(window).bind("load resize", function () {
                    var topOffset = 50,
                        width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
                    if (width < 768) {
                        $('div.navbar-collapse').addClass('collapse');
                        topOffset = 100; // 2-row-menu
                    } else {
                        $('div.navbar-collapse').removeClass('collapse');
                    }

                    var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1;
                    height = height - topOffset;
                    if (height < 1) height = 1;
                    if (height > topOffset) {
                        $("#page-wrapper").css("min-height", (height) + "px");
                    }
                });

            });


            $scope.location = $location;
            $scope.paths = [
                //{
                //    name: "产品管理",
                //    subPath: [{
                //        url: "/production",
                //        name: "产品列表"
                //    },
                //        {
                //            url: "/edit-production/new",
                //            name: "添加产品"
                //        },
                //        {
                //            url: "/classification",
                //            name: "分类管理"
                //        },
                //        {
                //            url: "/property",
                //            name: "属性管理"
                //        }
                //    ]
                //},
                //{
                //    name: "订单管理",
                //    subPath: [{
                //        url: "/order",
                //        name: "订单列表"
                //    },
                //        {
                //            url: "/edit-order/new",
                //            name: "添加订单"
                //        },
                //        {
                //            url: "/refund-order",
                //            name: "退换货列表"
                //        }
                //    ]
                //},
                //{
                //    name: "客户管理",
                //    subPath: [{
                //        url: "/customer",
                //        name: "客户列表"
                //    },
                //        {
                //            url: "/edit-customer/new",
                //            name: "添加用户"
                //        },
                //        {
                //            url: "/group",
                //            name: "分组管理"
                //        },
                //        {
                //            url: "/point-rule",
                //            name: "积分管理"
                //        }
                //    ]
                //},
                //{
                //    name: "营销管理",
                //    subPath: [{
                //        url: "/coupon",
                //        name: "优惠券管理"
                //       },
                //        {
                //            url: "/present",
                //            name: "赠品管理"
                //        },
                //        {
                //            url: "/rebeat",
                //            name: "返现管理"
                //        },
                //        {
                //            url: "/discount-activity",
                //            name: "折扣活动管理"
                //        },
                //
                //        {
                //            url: "/full-cut-activity",
                //            name: "满减活动管理"
                //        }
                //    ]
                //},
                {
                    name: "系统管理",
                    subPath: [{
                        url: "/user-role",
                        name: "角色管理"
                    },
                        {
                            url: "/user",
                            name: "人员管理"
                        },
                        {
                            url: "/config",
                            name: "参数设置"
                        },
                        {
                            url: "/search-key",
                            name: "关键字设置"
                        }
                    ]
                }

            ];


            if ($location.path() === "/") {
                $location.path('/production');
            }


            $scope.showLoading = function () {
                document.getElementById("loading").style.display = "block";
            };

            $scope.hideLoading = function () {
                document.getElementById("loading").style.display = "none";
            };

            $scope.isHandleSuccess = function (data) {
                return data.hasOwnProperty("code") && data.code === 0;
            };

            $scope.msg = {
                msg: null,
                show: function (data) {
                    var ctx = this;

                    var showMsg = function (msg, millsec, level) {
                        ctx.msg = msg;
                        ctx.level = level || "info";
                        $timeout(function () {
                            ctx.msg = null;
                        }, millsec);

                    };
                    if (typeof data == "string") {
                        showMsg(data, 5000, "warning");
                        return;
                    }
                    if ($scope.isHandleSuccess(data)) {
                        showMsg("操作成功", 2000);
                    } else {
                        var msg = data.error || "操作失败";
                        showMsg(msg, 5000, "warning");

                    }
                }
            };

        }]);

    }
);